#include <iostream>
#include <unistd.h>
#include <string>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/types.h>
#include <signal.h>
#include "common/Semaphore.h"
#include "json/json.h"
#include "application/Application.h"
#include "log/Logger.h"
#include "MyDB.h"
using namespace std;
CSemaphore g_Sem;
//信号初始化函数
void sigInit(void)
{
    struct sigaction sa;
    sa.sa_handler = SIG_IGN;
    sa.sa_flags = 0;
    //sigemptyset()用来将参数set信号集初始化并清空
    if (sigemptyset(&sa.sa_mask) == -1 ||
        sigaction(SIGPIPE, &sa, 0) == -1) 
    {
        LOG_ERROR("SigInit exit (-1)");
        exit(-1);
    }
}

int main(int argc, char *argv[])
{
    LOG_DEBUG("Entering main.cpp");
    //json 
    Json::Value root;
    //Json::FastWriter fast;
    std::shared_ptr<CApplication>  app = std::make_shared<CApplication>();
    app->init();
    root["ModuleType"] = Json::Value("Hello World");
    root["ModuleCode"] = Json::Value("200");
    root["ModuleDesc"] = Json::Value("33");
    root["DateTime"] = Json::Value("22222");
    LOG_INFO("output......" << root.toStyledString().c_str());
    

    //数据库调用
    MyDB db;
    //连接数据库
    db.initDB("localhost","root","wjj", "test");
    LOG_INFO(" =========  222222");
    //将用户信息添加到数据库
    db.exeSQL("INSERT accounts values('fengxin','123');");
    LOG_INFO(" =========  333333");
    db.exeSQL("INSERT accounts values('axin','123');");
    LOG_INFO(" =========  444444");
    //将所有用户信息读出，并输出
    db.exeSQL("SELECT * from accounts;");
    sigInit();

    g_Sem.Wait();
    return 0;
}